package cn.zgf.core;

import cn.zgf.utils.ContentUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * 读取excel的内容
 *
 * @author GaoFeng2017
 * @date 2018-02-20 14:48:06
 **/

public class ReadExcel {

    private String filePath;
    private Workbook workbook;

    public ReadExcel(String filePath) {
        this.filePath = filePath;
        init();
    }

    /**
     * @description:
     * @author: GaoFeng2017
     * @date: 2018/2/20 20:01
     * @param: []
     * @return: void
     *
     */
    private void init() {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(filePath);
            if (filePath.endsWith(".xls")) {
                this.workbook = new HSSFWorkbook(fileInputStream);
            } else if(filePath.endsWith(".xlsx")) {
               this.workbook = new XSSFWorkbook(fileInputStream);
            }

        } catch (Exception e) {
            throw new ExceptionInInitializerError(e);
        }
    }

    /**
     * @description:
     * @author: GaoFeng2017
     * @date: 2018/2/20 20:01
     * @param: [sheet]
     * @return: java.lang.String[]
     *
     */
    private String[] getHeads(Sheet sheet,int headIndex) {

        Row row = sheet.getRow(headIndex - 1);
        int count = row.getPhysicalNumberOfCells();
        String[] heads = new String[count];
        for (int i = 0;i < count;i++) {
            heads[i] = row.getCell(i).toString().replaceAll("\\s","");
            System.out.println(heads[i]);
        }

        return heads;
    }

    /**
     * @description:
     * @author: GaoFeng2017
     * @date: 2018/2/20 20:01
     * @param: []
     * @return: java.util.Map<java.lang.String,java.util.List<java.lang.String[]>>
     *
     */
    private List<String[]> getContent(Sheet sheet,int startRow) {
        int lastRowNum = sheet.getLastRowNum();
        List<String[]> sheetItems = new ArrayList<>(lastRowNum);
        for (int j = startRow - 1;j < lastRowNum;j++) {
            Row row = sheet.getRow(j);
            int cellNumber = row.getPhysicalNumberOfCells();
            String[] cells = new String[cellNumber];
            for (int k = 0;k < cellNumber;k++) {
                cells[k] = row.getCell(k) == null ? "" : row.getCell(k).toString();
            }
            if (!ContentUtils.isRowEmpty(cells)) {
                sheetItems.add(cells);
            }
        }
        return  sheetItems;
    }

    public <T> T read(int sheetIndex,int startRow,int headIndex,ContentHandler<T> handler) {
        Sheet sheet = this.workbook.getSheetAt(sheetIndex);
        List<String[]> content = this.getContent(sheet,startRow);
        String[] heads = this.getHeads(sheet,headIndex);
        return handler.handle(content,heads);
    }
}
